<?php

class Tag {
	static function validateAll($db="",&$tags,$res=array("state"=>true,"error"=>array())) {
		if($db=="")$db=DB::$db;
		if(!(isset($tags))){
			$res['state'] = false;	
			return $res;
		}
		foreach ($tags as $i => $tag){
			$res = Tag::validate($db,$tag,$i,$res);
		}
		return $res;
	}
	
	static function count_(){
		$res = DB::query("select count(*) as l from tag");
		return $res[0]['l'];
	}
	
	static function validate($db="",&$tag,$i,$res) {
		if($db=="")$db=DB::$db;
		if(is_int((int)$tag))return $res;
		if(!(isset($tag)&&strlen(utf8_decode($tag))<255)) {
			$res['state'] = false;
			$res['error']['tags['.$i.']'] = 1;
		}else if(empty($tag)){
			unset($tag);
		}else{
			$r = Tag::get("",$tag);
			if(count($r)!=0){
				print_r($r);
				$res['state'] = false;
				$res['error']['tags['.$i.']'] = 'Тег существует';
			}
		}
		return $res;
	}
	
	static function get($db="",$name="",$id=-1,$field="*",$where=""){
		try{
			$sql = "SELECT ".$field." FROM `tag`";
			
			if($where!=""){
				$sql.=" WHERE ".$where;
			}else if($name!=""){
				$sql.=" WHERE `name`='".$name."'";
			}else if($id>=0){
				$sql.=" WHERE `id`='".$id."'";
			}
			return DB::query($sql);
		} catch(PDOException $e) {
			$data['msg'] = "Error: " . $e -> errorInfo() . " sql: " . $sql;
			$data['state'] = false;
			echo json_encode($data);
		}
	}
	
	static function set($db="",$data,$name="",$id=-1){
		try{
			$sql  	= "update `tag` set ";
			$comma	= "";
			$where 	= "";
			if($name!=""){
				$where = " WHERE `name`='".$name."'";
			}else if($id>=0){
				$where = " WHERE `id`='".$id."'";
			}else{
				return false;
			}
			if(empty($data))return false;
			foreach ($data as $field => $value) {
				if(!empty($value)||$value==""){
					$sql.=$comma." `".$field."`='".$value."'";
					$comma = ",";
				}
			}
			$sql.=$where;
			//echo $sql."\n";
			DB::exec_($sql);
			return true;
		} catch(PDOException $e) {
			$data['msg'] = "Error: " . $e -> errorInfo() . " sql: " . $sql;
			$data['state'] = false;
			echo json_encode($data);
			return false;
		}
	}
	
	static function check($db="",$tag,$id){
		if($db=="")$db=DB::$db;
		$res = true;
		if((isset($tag)&&!empty($tag)&&strlen(utf8_decode($tag))>1&&strlen(utf8_decode($tag))<255)&&isset($id)) {
			$tag_id = Tag::get($db,$tag,-1,"`id`");
			if(!empty($tag_id)&&isset($tag_id[0])&&isset($tag_id[0]['id'])){
				$res = false;
				if($tag_id[0]['id']!=$id){
					return $tag_id[0]['id'];
				}
			}
		}
		return $res;
	}
	
	static function create($db="",$tags,$report="",$isNew=false){
		if($db=="")$db=DB::$db;
		$tags_ids 		= "";
		$comma 			= "";
		foreach ($tags as $tag){
			$tag_id 	= false;
			if($tag=="")continue;
			if(is_bool($isNew)&&$isNew){
				$db->exec('insert into `tag` (`name`,`reports`,`author`) values ("'.$tag.'","'.$report.'",\''.$_SESSION['user']['person']['id'].'\');');
				$id = Tag::get($db,$tag,-1,"`id`");
				if(isset($id[0])&&isset($id[0]['id'])){
					$tag_id = $id[0]['id'];
				}
			}else{
				$tag_id 	= $tag;
				$tag_data 	= Tag::get($db,"",$tag,"`reports`");
				if(isset($tag_data[0])&&isset($tag_data[0]['reports'])){
					$r = $report!=""?'`reports`=\''.($tag_data[0]['reports']!=""?$tag_data[0]['reports'].",":"").$report."'" : "";
					$db->exec('update `tag` set '.$r.' where `id`=\''.$tag_id."'");
				}
			}
			$tags_ids.=$comma.$tag_id;
			$comma=",";
		}
		return $tags_ids;
	}

	static function getViewData($tags="",$target="report",$role="view"){
		$result 	= "";
		$isEmpty	= false;
		if($tags==""){
			$tags = Tag::get();
		}else if($tags=="empty"){
			$isEmpty	= true;
			$tags 		= array(array("name"=>"","id"=>"","author"=>""),array("name"=>"","id"=>"","author"=>""));
		}
		if($role=="view"){
			$comma 	= "";
			foreach($tags as $tag){
				$tag['target'] = $target;
				$result.=$comma.Template::parse('tag/main',$tag);
				$comma="|";
			}
		}else if($role=='edit'){
			$tags_ = Tag::get();
			function getOpts($id="",$tags){
				$tags_options 	= "";
				foreach($tags as $i=>$tag){
					$tags_options.="<option value='".$tag['id']."'".($id==$tag['id']?" selected='selected'":"").">".$tag['name']."</option>";
				}
				return $tags_options;
			}
			$comma 			= "";
			foreach($tags as $i=>$tag){
				$tag['ind'] 		= "".$i;
				$tag['tags']		= getOpts($tag['id'],$tags_);
				$result.=$comma.Template::parse('tag/edit',$tag);
				$comma				= "";
			}
			$data['tags'] 	= $result;
			$data['addBut'] = Template::parse('tag/addButtton',$data);
			$result			= Template::parse('tag/list.edit',$data);
		}else if($role=='new'){
			$comma 		= "";
			foreach($tags as $i=>$tag){
				$disabled				= "";
				if($isEmpty)$autocomplete="";
				$tag['ind'] 		= "".$i;
				$result.=$comma.Template::parse('tag/new',$tag);
				$comma				= "";
			}
			$data['tags'] 	= $result;
			$data['addBut'] = Template::parse('tag/addButtton');
			$result			= Template::parse('tag/list.edit',$data);
		}else if($role=='grid'){
			$result			= Template::parse('tag/grid');
		}
		return $result;
	}
	
}
?>